#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
clean.py - clean dirs and files with regular expression
"""
import os
import re

from os import path
from shutil import rmtree

DEL_DIRS = None
DEL_FILES = r'(^\.DS_Store$)'


def del_dir(p):
    """Delete a directory."""
    if path.isdir(p):
        rmtree(p)
        print('D : %s' % p)


def del_file(p):
    """Delete a file."""
    if path.isfile(p):
        os.remove(p)
        print('F : %s' % p)


def gen_deletions(directory, del_dirs=DEL_DIRS, del_files=DEL_FILES):
    """Generate deletions."""
    patt_dirs = None if del_dirs is None else re.compile(del_dirs)
    patt_files = None if del_files is None else re.compile(del_files)

    for root, dirs, files in os.walk(directory):
        if patt_dirs:
            for d in dirs:
                if patt_dirs.match(d):
                    yield path.join(root, d)
        if patt_files:
            for f in files:
                if patt_files.match(f):
                    yield path.join(root, f)


def clean(directory):
    print('Processing...')
    m, n = 0, 0
    for p in gen_deletions(directory):
        if path.isdir(p):
            del_dir(p)
            m += 1
        elif path.isfile(p):
            del_file(p)
            n += 1
    print('Clean %d dirs and %d files.' % (m, n))


if __name__ == '__main__':
    import sys
    argv = sys.argv
    directory = argv[1] if len(argv) >= 2 else os.getcwd()
    clean(directory)
    # import subprocess
    # subprocess.call("pause", shell=True)
